<?php
declare (strict_types=1);

namespace app\controller;

use liliuwei\think\Jump;
use think\Request;
//use think\facade\Request;
use think\facade\Db;
use think\facade\View;

class Members
{
	use Jump;

	/**
	 * 显示资源列表
	 *
	 * @return \think\Response
	 */
	public function index()
	{


		$res = Db::name('members')->order("id", "desc")->paginate(5);
		return View('index', [
			"list"  => $res
		]);
	}

	/**
	 * 显示创建资源表单页.
	 *
	 * @return \think\Response
	 */
	public function create()
	{
		return View("add");
	}

	/**
	 * 保存新建的资源
	 *
	 * @param  \think\Request $request
	 *
	 * @return \think\Response
	 */
	public function save(Request $request)
	{

		$img = $request->file("image");
		//$savename = \think\facade\Filesystem::putFile( 'topic', $img);

		$imgpath = \think\facade\Filesystem::disk('public')->putFile('topic', $img);
		$imgpath = "/storage/" . $imgpath;
		$saveData = ['id'         => $request->post("id"), 'username' => $request->post("username"),
		             'email'      => $request->post("email"),
		             'mobile'     => $request->post("mobile"),
		             "image_path" => $imgpath
		];


		$validate = new \app\validate\Member;

		if(!$validate->check($saveData)) {
			dump($validate->getError());
			return;
		}

		if(Db::name('members')->save($saveData)) {
			//return redirect("/member/index");
			$this->success("保存成功！", "/member/index");
		}
		$this->success("保存成功！", "/member/index");
	}

	/**
	 * 显示指定的资源
	 *
	 * @param  int $id
	 *
	 * @return \think\Response
	 */
	public function read($id)
	{
		//
		return $id;
	}

	/**
	 * 显示编辑资源表单页.
	 *
	 * @param  int $id
	 *
	 * @return \think\Response
	 */
	public function edit($id)
	{
		$detail = Db::table('members')->where('id', $id)->find();
		return View('edit', [
			"detail" => $detail
		]);
	}

	/**
	 * 删除指定资源
	 *
	 * @param  int $id
	 *
	 * @return \think\Response
	 */
	public function delete($id)
	{
		if(!$id) {
			return "id不能为空";
		}
		Db::table('members')->delete($id);
		$this->success("删除成功！", "/member/index");
	}
}
